import os 
import subprocess
import sys



def createLink(srcPath,dstPath,extensions,includes = [],excludes=[]):
    
    files = os.listdir(srcPath)
    for file in files: 

        isExclude = False
        for exclude in excludes:
                if exclude in file:
                    isExclude = True
                    break
        if isExclude == True:
            continue



        if os.path.isfile(os.path.join(srcPath,file)):

            isInclude = False
            for include in includes:
                    if include in os.path.join(srcPath,file):
                        isExclude = True
                        break
            if isExclude == False:
                continue

            condition = False
            for extension in extensions:
                if str.endswith(file,extension):
                    condition = True
                    break

            if condition == True:
                if os.path.exists(os.path.join(dstPath,file)):
                    os.remove(os.path.join(dstPath,file))

                if os.path.exists(os.path.join(srcPath,file)):
                    os.symlink(os.path.join(srcPath,file),os.path.join(dstPath,file))
        else:
            createLink(os.path.join(srcPath,file),dstPath,includes,extensions)


if __name__ == '__main__':
    Drive = 'D'

    srcPath = Drive + ':/DevelopProj/Yuji/Projects/YBigModelProject/DownloadRoot/models/lllyasviel/sd_control_collection'
    dstPath = Drive + ':/DevelopProj/AI/stable-diffusion-webui/extensions/sd-webui-controlnet/models'
    extensions = ['safetensors']
    createLink(srcPath,dstPath,extensions)

    srcPath = Drive + ':/DevelopProj/Yuji/Projects/YBigModelProject/DownloadRoot/models'
    dstPath = Drive + ':/DevelopProj/AI/stable-diffusion-webui/models/Stable-diffusion'
    extensions = ['safetensors','ckpt']
    
    excludes = ['lllyasviel','OFA-Sys']
    includes = ['stabilityai','civitai','timbrooks']
    createLink(srcPath,dstPath,extensions,includes,excludes)

    
    

